package com.gewu.pm.ui.adapter.order;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.gewu.pm.R;
import com.gewu.pm.aop.SingleClick;
import com.gewu.pm.app.AppAdapter;
import com.gewu.pm.bean.GoodsBean;

/**
 * 商品适配器
 */
public class GoodsAdapter extends AppAdapter<GoodsBean> {

    private OnQuantityChangeListener mListener;
    private OnGoodsClickListener mGoodsClickListener;
    private boolean mShowDelete = false;  // 是否显示删除按钮

    public GoodsAdapter(Context context) {
        super(context);
    }

    public void setOnQuantityChangeListener(OnQuantityChangeListener listener) {
        mListener = listener;
    }

    public void setOnGoodsClickListener(OnGoodsClickListener listener) {
        mGoodsClickListener = listener;
    }

    /**
     * 设置是否显示删除按钮
     */
    public void setShowDelete(boolean showDelete) {
        mShowDelete = showDelete;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new GoodsViewHolder();
    }

    public final class GoodsViewHolder extends AppAdapter<?>.ViewHolder {

        protected final ImageView mDeleteView;
        protected final ImageView mImageView;
        protected final TextView mTagView;
        protected final TextView mNameView;
        protected final TextView mSellingPointView;
        protected final TextView mStockShelfLifeView;
        protected final TextView mCodeView;
        protected final TextView mPriceView;
        protected final TextView mDecreaseView;
        protected final TextView mQuantityView;
        protected final TextView mIncreaseView;

        public GoodsViewHolder() {
            super(R.layout.goods_item);
            mDeleteView = findViewById(R.id.iv_delete);
            mImageView = findViewById(R.id.iv_goods_image);
            mTagView = findViewById(R.id.tv_tag);
            mNameView = findViewById(R.id.tv_goods_name);
            mSellingPointView = findViewById(R.id.tv_selling_point);
            mStockShelfLifeView = findViewById(R.id.tv_stock_shelf_life);
            mCodeView = findViewById(R.id.tv_code);
            mPriceView = findViewById(R.id.tv_price);
            mDecreaseView = findViewById(R.id.tv_decrease);
            mQuantityView = findViewById(R.id.tv_quantity);
            mIncreaseView = findViewById(R.id.tv_increase);

            // 设置商品点击
            getItemView().setOnClickListener(v -> onGoodsClick(getViewHolderPosition()));

            // 设置加减按钮点击（增大padding提升灵敏度）
            mDecreaseView.setOnClickListener(v -> onDecreaseClick(getViewHolderPosition()));
            mIncreaseView.setOnClickListener(v -> onIncreaseClick(getViewHolderPosition()));
            
            // 设置删除按钮点击
            mDeleteView.setOnClickListener(v -> onDeleteClick(getViewHolderPosition()));
        }
        
        private void onGoodsClick(int position) {
            if (mGoodsClickListener != null) {
                mGoodsClickListener.onGoodsClick(position);
            }
        }

        @Override
        public void onBindView(int position) {
            GoodsBean goods = getItem(position);

            // 删除按钮（根据设置显示/隐藏）
            mDeleteView.setVisibility(mShowDelete ? View.VISIBLE : View.GONE);

            // 商品图片（暂时用logo占位）
            mImageView.setImageResource(R.drawable.logo_big_ic);

            // 促销标签
            if (!TextUtils.isEmpty(goods.getTag())) {
                mTagView.setVisibility(View.VISIBLE);
                mTagView.setText(goods.getTag());
            } else {
                mTagView.setVisibility(View.GONE);
            }

            // 商品名称
            mNameView.setText(goods.getName());

            // 卖点
            mSellingPointView.setText(goods.getSellingPoint());

            // 库存和保质期
            String stockShelfLife = getContext().getString(R.string.stock) + goods.getStock() + goods.getUnit() +
                    "  " + getContext().getString(R.string.shelf_life) + goods.getShelfLife();
            mStockShelfLifeView.setText(stockShelfLife);

            // 编码
            mCodeView.setText(getContext().getString(R.string.code) + " " + goods.getCode());

            // 价格
            mPriceView.setText(goods.getPrice());

            // 数量
            mQuantityView.setText(String.valueOf(goods.getQuantity()));

            // 减号按钮状态（数量为0时禁用）
            mDecreaseView.setEnabled(goods.getQuantity() > 0);
        }

        @SingleClick(value = 300)
        protected void onDecreaseClick(int position) {
            GoodsBean goods = getItem(position);
            
            if (goods.decreaseQuantity()) {
                // 减少成功
                notifyItemChanged(position);
                if (mListener != null) {
                    mListener.onQuantityChanged();
                }
            }
        }

        @SingleClick(value = 300)
        protected void onIncreaseClick(int position) {
            GoodsBean goods = getItem(position);
            
            if (goods.increaseQuantity()) {
                // 增加成功
                notifyItemChanged(position);
                if (mListener != null) {
                    mListener.onQuantityChanged();
                }
            } else {
                // 库存不足
                if (mListener != null) {
                    mListener.onStockInsufficient(goods.getName());
                }
            }
        }

        @SingleClick
        protected void onDeleteClick(int position) {
            if (mListener != null) {
                mListener.onDeleteGoods(position);
            }
        }
    }

    /**
     * 数量变化回调接口
     */
    public interface OnQuantityChangeListener {
        /** 数量变化 */
        void onQuantityChanged();
        
        /** 库存不足 */
        void onStockInsufficient(String goodsName);
        
        /** 删除商品 */
        void onDeleteGoods(int position);
    }

    /**
     * 商品点击回调接口
     */
    public interface OnGoodsClickListener {
        /** 商品点击 */
        void onGoodsClick(int position);
    }
}

